# -*- coding: utf-8 -*-
import json
import requests

from flask import request
from flask_restx import Resource, Namespace, fields

from utils.response import ResponseMaker
from app.models.user import User
from app.extentions import db
from utils.jwt import JWTUtil
from utils.ai import RobotAI, PictureAI


ai_api = Namespace("ai", description="AI功能", path="/api/ai")

ai_picture_model = ai_api.model('AIPictureModel', {
    'picture_path': fields.String(required=True, description='图片存储在服务器上的绝对路径'),
})

ai_robot_model = ai_api.model('AIRobotModel', {
    'question': fields.String(required=True, description='发送给AI机器人的对话内容'),
})


@ai_api.route('/picture')
class PictureAPI(Resource):

    @ai_api.doc(description='AI识图', body=ai_picture_model)
    @JWTUtil.verify_token_decorator(request)
    def post(self, *args, **kwargs):
        data = json.loads(request.data)
        picture_path = data.get("picture_path")

        picture = PictureAI()
        result= picture.recognize_picture(picture_path)

        return ResponseMaker.success(data=result)


@ai_api.route('/robot')
class RobotAPI(Resource):

    @ai_api.doc(description='AI聊天机器人', body=ai_robot_model)
    @JWTUtil.verify_token_decorator(request)
    def post(self, *args, **kwargs):
        data = json.loads(request.data)
        question = data.get("question")

        robot = RobotAI()
        result = robot.get_anwswer(question)

        return ResponseMaker.success(data=result)